package com.aigou.controller;

import com.aigou.exception.AigouException;
import com.aigou.exception.LoginException;
import com.aigou.pojo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CommonException {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result ex(Exception e) {
        e.printStackTrace();
        Result result = new Result();
        if (e instanceof AigouException) {
            result.setCode(20001);
            result.setMsg(e.getMessage());
        } else if (e instanceof LoginException) {
            result.setCode(20003);
            result.setMsg(e.getMessage());
        } else {
            result.setCode(99999);
            result.setMsg("系统错误请联系管理员");
        }
        return result;
    }
}
